//
//  AboutHedron.m
//  iHedron
//
//  Created by George Radev on 2011-08-13.
//  Copyright 2011 I Me Mine. All rights reserved.
//

#import "AboutHedron.h"
#import "HedronCore.h"

@implementation AboutHedron

- (void)viewDidLoad {
    [super viewDidLoad];
	self.title = @"iHedron";
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}

#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 3;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    if (section == 0) {
		return 1;
	} else if (section == 1) {
		return 1; // more reports to come
	}else if(section == 2){
		if([HedronCore isHTTPStarted]){
			return 2; //with status line
		}else{
			return 1; 
		}
	}else{
		return 0;
	}
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	static NSString *HeaderCell = @"HeaderCell";
	static NSString *Cell = @"Cell";
	
	int section = indexPath.section;
	int row = indexPath.row;
    
	if (section == 0) {
		UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:HeaderCell];
		if (cell == nil) {
			cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:HeaderCell] autorelease];
		}
		
		cell.textLabel.text = @"iHedron Lite v1.0";
		cell.detailTextLabel.text = @"GeorgeRadev@gmail.com";
		cell.imageView.image = [UIImage imageNamed:@"icon.png"];
		
		return cell;
		
	}else if (section == 1) {
		UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Cell];
		if (cell == nil) {
			cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Cell] autorelease];
		}
		
		cell.textLabel.text = @"--more reports to come --";
		
		return cell;
		
	} else {
		UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Cell];
		if (cell == nil) {
			cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Cell] autorelease];
		}
		if(row == 0){
			cell.textLabel.text = [HedronCore isHTTPStarted] ? @"Спри сървъра !" : @"Стартирай !";
		}else{
			cell.textLabel.text = [HedronCore HTTPMessage];
		}
		
		return cell;		
	}	
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 40;
}

-(UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UILabel* headerLabel = [[UILabel alloc] init];
    headerLabel.frame = CGRectMake(10, 0, 300, 40);
    headerLabel.backgroundColor = [UIColor clearColor];
	if (section == 0) {
		headerLabel.text = @"   За програмата";
	}else if (section == 1) {
		headerLabel.text = @"   Справки";
	}else{
		headerLabel.text = @"   Web Server";		
	}
	
	[headerLabel autorelease];
    return headerLabel;
	
}

#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    int section = indexPath.section;
	int row = indexPath.row;
	
	if(section == 2 && row == 0){
		if([HedronCore isHTTPStarted]){
			[HedronCore stopHTTP];
		}else{
			[HedronCore startHTTP];
		}
		/*
		UIAlertView *alert = [[UIAlertView alloc] init];
		[alert setTitle:@"Web!"];
		[alert setMessage:@"Web ON!"];
		[alert setDelegate:self];
		[alert addButtonWithTitle:@"Ok"];
		[alert show];
		[alert release];
		*/
		[self.tableView reloadData];
		
	}
}


#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Relinquish ownership any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end
